package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.happy.cat.common.utils.StringUtils;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.UserPosting;
import com.happy.cat.project.system.service.IUserPostingService;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.common.utils.poi.ExcelUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.happy.cat.framework.web.page.TableDataInfo;

/**
 * 用户帖子动态消息Controller
 *
 * @author verne
 * @date 2020-08-16
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/userPosting" )
@Api("用户帖子动态消息 API")
public class UserPostingController extends BaseController {

    private final IUserPostingService iUserPostingService;

    /**
     * 查询用户帖子动态消息列表
     */
    @ApiOperation(value = "查询用户帖子动态消息列表",notes = "查询用户帖子动态消息列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:userPosting:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserPosting userPosting)
    {
        startPage();
        LambdaQueryWrapper<UserPosting> lqw = new LambdaQueryWrapper<UserPosting>();
        if (StringUtils.isNotBlank(userPosting.getContent())){
            lqw.eq(UserPosting::getContent ,userPosting.getContent());
        }
        if (StringUtils.isNotBlank(userPosting.getTitle())){
            lqw.eq(UserPosting::getTitle ,userPosting.getTitle());
        }
        if (userPosting.getType() != null){
            lqw.eq(UserPosting::getType ,userPosting.getType());
        }
        if (userPosting.getAccountId() != null){
            lqw.eq(UserPosting::getAccountId ,userPosting.getAccountId());
        }
        if (userPosting.getSendTime() != null){
            lqw.eq(UserPosting::getSendTime ,userPosting.getSendTime());
        }
        if (userPosting.getCircleId() != null){
            lqw.eq(UserPosting::getCircleId ,userPosting.getCircleId());
        }
        if (StringUtils.isNotBlank(userPosting.getPhotoMessagePath())){
            lqw.eq(UserPosting::getPhotoMessagePath ,userPosting.getPhotoMessagePath());
        }
        if (userPosting.getParentId() != null){
            lqw.eq(UserPosting::getParentId ,userPosting.getParentId());
        }
        if (userPosting.getTopicId() != null){
            lqw.eq(UserPosting::getTopicId ,userPosting.getTopicId());
        }
        if (userPosting.getApplyTime() != null){
            lqw.eq(UserPosting::getApplyTime ,userPosting.getApplyTime());
        }
        if (StringUtils.isNotBlank(userPosting.getApplyBy())){
            lqw.eq(UserPosting::getApplyBy ,userPosting.getApplyBy());
        }
        if (StringUtils.isNotBlank(userPosting.getApplyText())){
            lqw.eq(UserPosting::getApplyText ,userPosting.getApplyText());
        }
        if (userPosting.getStatus() != null){
            lqw.eq(UserPosting::getStatus ,userPosting.getStatus());
        }
        List<UserPosting> list = iUserPostingService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出用户帖子动态消息列表
     */
    @ApiOperation(value = "导出用户帖子动态消息列表",notes = "导出用户帖子动态消息列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userPosting:export')" )
    @Log(title = "用户帖子动态消息" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(UserPosting userPosting) {
        LambdaQueryWrapper<UserPosting> lqw = new LambdaQueryWrapper<UserPosting>(userPosting);
        List<UserPosting> list = iUserPostingService.list(lqw);
        ExcelUtil<UserPosting> util = new ExcelUtil<UserPosting>(UserPosting. class);
        return util.exportExcel(list, "userPosting" );
    }

    /**
     * 获取用户帖子动态消息详细信息
     */
    @ApiOperation(value = "获取用户帖子动态消息详细信息",notes = "获取用户帖子动态消息详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userPosting:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iUserPostingService.getById(id));
    }

    /**
     * 新增用户帖子动态消息
     */
    @ApiOperation(value = "新增用户帖子动态消息",notes = "新增用户帖子动态消息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userPosting:add')" )
    @Log(title = "用户帖子动态消息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserPosting userPosting) {
        return toAjax(iUserPostingService.save(userPosting) ? 1 : 0);
    }

    /**
     * 修改用户帖子动态消息
     */
    @ApiOperation(value = "修改用户帖子动态消息",notes = "修改用户帖子动态消息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userPosting:edit')" )
    @Log(title = "用户帖子动态消息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserPosting userPosting) {
        return toAjax(iUserPostingService.updateById(userPosting) ? 1 : 0);
    }

    /**
     * 删除用户帖子动态消息
     */
    @ApiOperation(value = "删除用户帖子动态消息",notes = "删除用户帖子动态消息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userPosting:remove')" )
    @Log(title = "用户帖子动态消息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iUserPostingService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
